import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('@/views/Dashboard.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        redirect: '/dashboard/project-overview'
      },
      {
        path: 'project-overview',
        name: 'ProjectOverview',
        component: () => import('@/views/ProjectOverview.vue'),
        meta: { title: '项目总览' }
      },
      {
        path: 'project-details/:id',
        name: 'ProjectDetails',
        component: () => import('@/views/ProjectDetails.vue'),
        meta: { title: '项目详情' }
      },
      {
        path: 'basic-info',
        name: 'BasicInfo',
        component: () => import('@/views/BasicInfo.vue'),
        meta: { title: '基本信息' }
      },
      {
        path: 'document-list',
        name: 'DocumentList',
        component: () => import('@/views/DocumentList.vue'),
        meta: { title: '文档列表' }
      },
      {
        path: 'member-info',
        name: 'MemberInfo',
        component: () => import('@/views/MemberInfo.vue'),
        meta: { title: '成员信息' }
      },
      {
        path: 'progress-tracking',
        name: 'ProgressTracking',
        component: () => import('@/views/ProgressTracking.vue'),
        meta: { title: '进度跟踪' }
      },
      {
        path:'profile',
        name:'profile',
        component:()=>import('@/views/Profile.vue'),
        meta:{title:'个人信息'}
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next('/login')
  } else if (to.path === '/login' && userStore.isLoggedIn) {
    next('/dashboard')
  } else {
    next()
  }
})

export default router
